<style>
    #add {
        padding: 20px 25px 25px 0;
    }

    #add .layui-treeSelect .ztree li a, .ztree li span {
        margin: 0 0 2px 3px !important;
    }
</style>
<div class="layui-fluid" id="add">
    <form class="layui-form" action="" id="user-add-form" lay-filter="user-add-form"  enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">实验名称</label>
            <div class="layui-input-block">
                <input type="text" name="projectName" lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属实验课程</label>
            <div class="layui-input-block">
                <input type="text" name="courseName"  id="experiment_table" placeholder="请选择实验课程" lay-filter="experiment_table" class="layui-input">
                <input type="hidden" id="courseId">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">实验描述</label>
            <div class="layui-input-block">
                <input type="hidden" name="remark" id="remark">
                <div id="editor">
                    <p>请输入 <b>实验描述</b></p>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">实验报告</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="upload_file">
                    <i class="layui-icon">&#xe67c;</i>上传附件
                </button>
                <div id="result"></div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">状态：</label>
            <div class="layui-input-block">
                <input type="radio" name="state" value="1" title="有效" checked="">
                <input type="radio" name="state" value="0" title="禁用">
            </div>
        </div>

        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="user-add-form-submit" id="submit"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
        </div>
    </form>
</div>
<script type="text/javascript" th:src="@{febs/lay/extends/wangEditor.min.js}"></script>
<script>
    layui.use(['febs', 'form', 'tableSelect','upload', 'validate'], function () {
        var $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            $view = $('#add'),
            upload = layui.upload,
            tableSelect = layui.tableSelect,
            validate = layui.validate;

        form.verify(validate);
        form.render();

        var E = window.wangEditor;
        var editor = new E('#editor');
        // 或者 var editor = new E( document.getElementById('editor') )
        editor.create();


        upload.render({
            elem: '#upload_file', //绑定元素
            accept: "file",
            exts:'docx',
            // acceptMime: 'image/*',//打开文件选择框时,只显示图片文件
            auto: false,  //是否选完文件后自动上传。默认值：true
            // bindAction: '#btnSub',
            choose: function (obj) {
                obj.preview(function (index, file, result) {
                    // $('#headImage').attr('src', result);
                    $("#result").html(file.name);
                })
            }
        });

        tableSelect.render({
            elem: '#experiment_table',	//定义输入框input对象 必填
            checkedKey: 'courseName', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'courseName',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '请输入实验课程',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: ctx + 'course/select/course',
                cols: [
                    [
                        { type: 'radio' },
                        {field: 'courseName', title: '课程名称', minWidth: 100},
                        {field: 'deptNameList', title: '班级名称列表', align: 'center', width: 300},
                    ]
                ]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var NEWJSON = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.courseName);
                    $("#experimentId").val(item.courseId);
                });
                elem.val(NEWJSON.join(","))
            }
        });


        form.on('submit(user-add-form-submit)', function (data) {
            data.field.projectContent = editor.txt.text();
            var formData = new FormData(document.getElementById("user-add-form"));
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            formData.set("projectContent",data.field.projectContent);
            $.ajax({
                url: ctx + 'experimentProject/add',
                data: formData,
                type: 'post',
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg('操作成功', {
                            icon: 6,
                            time: 1000
                        }, function () {
                            layer.closeAll();
                            $('#febs-user').find('#query').click();
                        });
                    } else {
                        layer.msg('操作失败,请重新上传', {
                            icon: 5,
                            time: 1000
                        });
                    }
                },
                error: function (data) {
                    layer.msg('操作失败', {
                        icon: 5,
                        time: 1000
                    });
                }
            });

        /*    febs.post(ctx + 'experimentProject/add', data.field, function () {
                layer.closeAll();
                febs.alert.success('新增项目成功！');
                $('#febs-user').find('#query').click();
            });*/
            return false;
        });
    });
</script>